package com.ycfmedical.bed.ui.fragment;

import java.util.List;

import com.ycfmedical.bed.R;
import com.ycfmedical.bed.bean.EntertainItem;
import com.ycfmedical.bed.present.EntertainPresenter;
import com.ycfmedical.bed.widgets.StateView;

import android.os.Bundle;
import android.view.View;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.SimpleRecAdapter;
import cn.droidlover.xdroidmvp.mvp.XFragment;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;

/**
 * Created by Egoist on 2017/1/19.
 */

public abstract class EntertainFragment extends XFragment<EntertainPresenter>
{
    StateView errorView;
    
    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;
    
    @Override
    public void initData(Bundle savedInstanceState)
    {
        initAdapter();
        getP().loadData(getType());//读取本地数据
    }
    
    private void initAdapter()
    {
        setLayoutManager(contentLayout.getRecyclerView());
        
        contentLayout.getRecyclerView().setAdapter(getAdapter());
        
        contentLayout.getRecyclerView().setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener()
        {
            @Override
            public void onRefresh()
            {
                getP().loadData(getType());
                getvDelegate().toastShort("没有更多数据");
            }
            
            @Override
            public void onLoadMore(int page)
            {
                //getP().handleMessage(getType());
                getvDelegate().toastShort("没有更多数据");
            }
        });
        
        if (errorView == null)
        {
            errorView = new StateView(context);
        }
        contentLayout.errorView(errorView);
        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        contentLayout.getRecyclerView().useDefLoadMoreView();
        
    }
    
    public void showData(List<EntertainItem> model)
    {
        getAdapter().addData(model);
        if (getAdapter().getItemCount() < 1)
        {
            contentLayout.showEmpty();
            return;
        }
        else
        {
            getAdapter().setData(model);
        }
        
    }
    

    public abstract SimpleRecAdapter getAdapter();
    
    public abstract void setLayoutManager(XRecyclerView recyclerView);
    
    public abstract String getType();
    
    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_base_entertain;
    }
    
    @Override
    public EntertainPresenter newP()
    {
        return new EntertainPresenter(context);
    }
    
}
